﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Hi_Baidu.Notify.NotifyObject;
using Hi_Baidu.Packet.InPacket;

namespace Hi_Baidu.ProcessModel
{
    class FriendProcessModel : Model
    {
        private static byte[] PackageBody;
        private static byte[] protocol;

        /// <summary>
        ///     向服务器发送获取指定用户名的好友信息请求包
        /// </summary>
        /// <param name="FriendUsername">好友用户名</param>
        /// <param name="username">自身用户名</param>
        public static void GetPostFriendInfo(byte[] FriendUsername, byte[] username)
        {
            Offset = 0;
            if (username.Length > 0 && FriendUsername.Length > 0)
            {
                byte[] UsernameLength = BitConverter.GetBytes(username.Length);
                byte[] FriendnameLength = BitConverter.GetBytes(FriendUsername.Length);
                byte[] IPAddress = Encoding.Default.GetBytes(Dns.GetHostAddresses(Dns.GetHostName())[0].ToString());
                byte[] IPLength = BitConverter.GetBytes(IPAddress.Length);
                byte[] TalkPort = BitConverter.GetBytes(Globle.user.TALK_PORT);
                byte[] None = new byte[2];
                byte[] PackageBody = new byte[username.Length + FriendUsername.Length + None.Length + IPAddress.Length + 16];

                //用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, UsernameLength.Length);
                Offset += 4;
                //用户名
                Array.ConstrainedCopy(username, 0, PackageBody, Offset, username.Length);
                Offset += username.Length;
                //好友用户名长度
                Array.ConstrainedCopy(FriendnameLength, 0, PackageBody, Offset, FriendnameLength.Length);
                Offset += 4;
                //好友用户名
                Array.ConstrainedCopy(FriendUsername, 0, PackageBody, Offset, FriendUsername.Length);
                Offset += FriendUsername.Length;
                //空白 
                Array.ConstrainedCopy(None, 0, PackageBody, Offset, None.Length);
                Offset += 2;
                //IP地址长度
                Array.ConstrainedCopy(IPLength, 0, PackageBody, Offset, IPLength.Length);
                Offset += 4;
                //IP地址
                Array.ConstrainedCopy(IPAddress, 0, PackageBody, Offset, IPAddress.Length);
                Offset += IPAddress.Length;
                //聊天端口
                Array.ConstrainedCopy(TalkPort, 0, PackageBody, Offset, TalkPort.Length);

                protocol = ProtocolFactory.ProtocolFactory.GetProtocol("GetPostFriendInfo",
                                                                       (int)Packet.Protocol.OUT_NetworkCommand.GET_POST_FRIEND_INFO_PACKAGE,
                                                                       (int)Packet.Protocol.NetworkProtocol.Base, PackageBody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       Globle.user.SESSION_CODE);
                //发送请求
                NetControl.Send(protocol, 8700, ServerIp);
            }
        }

        /// <summary>
        ///     添加指定用户为好友
        /// </summary>
        /// <param name="FriendUsername">好友用户名</param>
        /// <param name="username">自身用户名</param>
        /// <param name="ItemId">添加到的分组ID</param>
        public static void AddFriend(byte[] FriendUsername, byte[] username, byte[] ItemId)
        {
            Offset = 0;
            if (username.Length > 0 && FriendUsername.Length > 0 && ItemId.Length > 0)
            {
                byte[] UsernameLength = BitConverter.GetBytes(username.Length);
                byte[] FriendUsernameLength = BitConverter.GetBytes(FriendUsername.Length);
                byte[] IPAddress = Encoding.Default.GetBytes(Dns.GetHostAddresses(Dns.GetHostName())[0].ToString());
                byte[] IPLength = BitConverter.GetBytes(IPAddress.Length);
                byte[] TalkPort = BitConverter.GetBytes(Globle.user.TALK_PORT);
                byte[] None = new byte[2];
                byte[] PackageBody = new byte[username.Length + FriendUsername.Length + None.Length + IPAddress.Length + 20];
                //用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, UsernameLength.Length);
                Offset += 4;
                //用户名
                Array.ConstrainedCopy(username, 0, PackageBody, Offset, username.Length);
                Offset += username.Length;
                //分组ID
                Array.ConstrainedCopy(ItemId, 0, PackageBody, Offset, ItemId.Length);
                Offset += 4;
                //好有用户名长度
                Array.ConstrainedCopy(FriendUsernameLength, 0, PackageBody, Offset, FriendUsernameLength.Length);
                Offset += 4;
                //好有用户名
                Array.ConstrainedCopy(FriendUsername, 0, PackageBody, Offset, FriendUsername.Length);
                Offset += FriendUsername.Length;
                //空白 
                Array.ConstrainedCopy(None, 0, PackageBody, Offset, None.Length);
                Offset += 2;
                //IP地址长度
                Array.ConstrainedCopy(IPLength, 0, PackageBody, Offset, IPLength.Length);
                Offset += 4;
                //IP地址
                Array.ConstrainedCopy(IPAddress, 0, PackageBody, Offset, IPAddress.Length);
                Offset += IPAddress.Length;
                //聊天端口
                Array.ConstrainedCopy(TalkPort, 0, PackageBody, Offset, TalkPort.Length);
                protocol = ProtocolFactory.ProtocolFactory.GetProtocol("AddFriend",
                                                                       (int)Packet.Protocol.OUT_NetworkCommand.ADD_FRIEND,
                                                                       (int)Packet.Protocol.NetworkProtocol.Base, PackageBody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       Globle.user.SESSION_CODE);
                //发送请求
                NetControl.Send(protocol, 8700, ServerIp);
            }
        }

        /// <summary>
        ///     向服务器提交添加好友的结果
        /// </summary>
        /// <param name="Result">添加好友状态</param>
        /// <param name="NotifyObject">添加好友提示对象</param>
        /// <param name="username">自身用户名</param>
        /// <param name="ItemId">要添加到的分组</param>
        public static void PostAddFriendResult(Hi_Baidu.ProcessModel.FriendProcessModel.ADDFRIEND_RESULT Result, Notify.Notify NotifyObject, byte[] username, byte[] FriendItemId)
        {
            if (username.Length > 0 && FriendItemId.Length > 0)
            {
                byte[] UsernameLength = BitConverter.GetBytes(username.Length);
                byte[] Friendname = Encoding.Default.GetBytes((NotifyObject as Notify_AddFriend).GetUsername());
                byte[] FriendnameLength = BitConverter.GetBytes(Friendname.Length);
                byte[] IPAddress = Encoding.Default.GetBytes(Dns.GetHostAddresses(Dns.GetHostName())[0].ToString());
                byte[] IPLength = BitConverter.GetBytes(IPAddress.Length);
                byte[] TalkPort = BitConverter.GetBytes(Globle.user.TALK_PORT);
                byte[] None = new byte[2];
                byte[] ItemID = BitConverter.GetBytes(int.Parse((NotifyObject.NotifyPackage as RedirectAddFriendPacket).GetItemId()));
                byte[] PackageBody = new byte[username.Length + Friendname.Length + None.Length + IPAddress.Length + 25];
                Offset = 0;
                //用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, UsernameLength.Length);
                Offset += 4;
                //用户名
                Array.ConstrainedCopy(username, 0, PackageBody, Offset, username.Length);
                Offset += username.Length;
                //好有用户名长度
                Array.ConstrainedCopy(FriendnameLength, 0, PackageBody, Offset, FriendnameLength.Length);
                Offset += 4;
                //好有用户名
                Array.ConstrainedCopy(Friendname, 0, PackageBody, Offset, Friendname.Length);
                Offset += Friendname.Length;
                //分组ID
                Array.ConstrainedCopy(ItemID, 0, PackageBody, Offset, ItemID.Length);
                Offset += 4;
                //空白 
                Array.ConstrainedCopy(None, 0, PackageBody, Offset, None.Length);
                Offset += 2;
                if (Result == ADDFRIEND_RESULT.SUCESSFUL)
                {
                    byte[] result = BitConverter.GetBytes(true);
                    //结果判断
                    Array.ConstrainedCopy(result, 0, PackageBody, Offset, result.Length);
                    Offset += 1;
                    //分组ID
                    Array.ConstrainedCopy(FriendItemId, 0, PackageBody, Offset, FriendItemId.Length);
                    Offset += 4;
                }
                else
                {
                    byte[] result = BitConverter.GetBytes(false);
                    //结果判断
                    Array.ConstrainedCopy(result, 0, PackageBody, Offset, result.Length);
                    Offset += 1;
                    //分组ID
                    Array.ConstrainedCopy(FriendItemId, 0, PackageBody, Offset, FriendItemId.Length);
                    Offset += 4;
                }
                //IP地址长度
                Array.ConstrainedCopy(IPLength, 0, PackageBody, Offset, IPLength.Length);
                Offset += 4;
                //IP地址
                Array.ConstrainedCopy(IPAddress, 0, PackageBody, Offset, IPAddress.Length);
                Offset += IPAddress.Length;
                //聊天端口
                Array.ConstrainedCopy(TalkPort, 0, PackageBody, Offset, TalkPort.Length);
                protocol = ProtocolFactory.ProtocolFactory.GetProtocol("PostAddFriendResult",
                                                                       (int)Packet.Protocol.OUT_NetworkCommand.POST_ADD_FRIEND_RESULT_PAKCAGE,
                                                                       (int)Packet.Protocol.NetworkProtocol.Base, PackageBody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       Globle.user.SESSION_CODE);
                //发送请求
                NetControl.Send(protocol, 8700, ServerIp);
            }
        }

        /// <summary>
        ///     向服务器发送删除好友请求包
        /// </summary>
        /// <param name="username">自身用户名</param>
        /// <param name="FriendName">好友用户名</param>
        /// <param name="ItemId">分组ID</param>
        public static void DeleteFriend(byte[] username, byte[] FriendName, byte[] ItemId)
        {
            Offset = 0;
            if (username.Length > 0 && FriendName.Length > 0 && ItemId.Length > 0)
            {
                byte[] UsernameLength = BitConverter.GetBytes(username.Length);
                byte[] FriendnameLength = BitConverter.GetBytes(FriendName.Length);
                byte[] IPAddress = Encoding.Default.GetBytes(Dns.GetHostAddresses(Dns.GetHostName())[0].ToString());
                byte[] IPLength = BitConverter.GetBytes(IPAddress.Length);
                byte[] TalkPort = BitConverter.GetBytes(Globle.user.TALK_PORT);
                byte[] None = new byte[2];
                byte[] PackageBody = new byte[username.Length + FriendName.Length + None.Length + IPAddress.Length + 20];

                //用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, UsernameLength.Length);
                Offset += 4;
                //用户名
                Array.ConstrainedCopy(username, 0, PackageBody, Offset, username.Length);
                Offset += username.Length;
                //好有用户名长度
                Array.ConstrainedCopy(FriendnameLength, 0, PackageBody, Offset, FriendnameLength.Length);
                Offset += 4;
                //好有用户名
                Array.ConstrainedCopy(FriendName, 0, PackageBody, Offset, FriendName.Length);
                Offset += FriendName.Length;              
                //分组ID
                Array.ConstrainedCopy(ItemId, 0, PackageBody, Offset, ItemId.Length);
                Offset += 4;
                //空白 
                Array.ConstrainedCopy(None, 0, PackageBody, Offset, None.Length);
                Offset += 2;
                //IP地址长度
                Array.ConstrainedCopy(IPLength, 0, PackageBody, Offset, IPLength.Length);
                Offset += 4;
                //IP地址
                Array.ConstrainedCopy(IPAddress, 0, PackageBody, Offset, IPAddress.Length);
                Offset += IPAddress.Length;
                //聊天端口
                Array.ConstrainedCopy(TalkPort, 0, PackageBody, Offset, TalkPort.Length);
                protocol = ProtocolFactory.ProtocolFactory.GetProtocol("DeleteFriend",
                                                                       (int)Packet.Protocol.OUT_NetworkCommand.DELETE_FRIEND_PACKAGE,
                                                                       (int)Packet.Protocol.NetworkProtocol.Base, PackageBody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       Globle.user.SESSION_CODE);
                //发送请求
                NetControl.Send(protocol, 8700, ServerIp);
            }
        }

        /// <summary>
        ///     添加好友结果
        /// </summary>
        public enum ADDFRIEND_RESULT
        {
            /// <summary>
            ///     允许添加
            /// </summary>
            SUCESSFUL,
            /// <summary>
            ///     拒绝添加
            /// </summary>
            FAIL
        }
    }
}
